/*
 *    Webical - http://www.webical.org
 *    Copyright (C) 2007 Func. Internet Integration
 *
 *    This file is part of Webical.
 *
 *    $Id: DayViewPanel.java 247 2012-07-29 19:42:54Z hz0885@gmail.com $
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.webical.web.component.calendar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.GregorianCalendar;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.webical.util.CalendarUtils;
import org.webical.web.action.AddEventAction;
import org.webical.web.action.IAction;
import org.webical.web.component.calendar.model.EventsModel;
import org.webical.web.component.calendar.model.WrappingEventsModel;

/**
 * Panel displays the events of the user selected calendars for a particular day
 *
 * @author Mattijs Hoitink
 * @author Harm-Jan Zwinderman, Cebuned
 */
public abstract class DayViewPanel extends CalendarViewPanel {
	private static final long serialVersionUID = 1L;

	// Markup ID's
	private static final String DAY_HEADING_LABEL_MARKUP_ID = "dayLink";
	private static final String ADD_EVENT_LINK_MARKUP_ID = "addEventLink";
	private static final String EVENT_ITEM_MARKUP_ID = "eventItem";

	/** Contains the actions this panel can handle */
	protected static Class[] PANELACTIONS = new Class[] { };

	// The date format
	private DateFormat dateFormat;

	/** The EventsModel for this panel */
	private EventsModel eventsModel;

	// Panel components
	private Label dayHeadingLabel;
	private Link addEventLink;
	private EventsListView dayEventsListView;

	/**
	 * Constructor
	 * @param markupId - The ID used in markup
	 * @param daysToShow - Days to show (1)
	 * @param currentDate - The currentDate
	 */
	public DayViewPanel(String markupId, int daysToShow, GregorianCalendar currentDate) {
		super(markupId, currentDate, DayViewPanel.class);

		setViewPeriodId(GregorianCalendar.DAY_OF_MONTH);
		setViewPeriodLength(daysToShow);
		// Set the start/end date for the range
		setPeriodStartDate(CalendarUtils.getStartOfDay(getViewCurrentDate().getTime()));
		setPeriodEndDate(CalendarUtils.getEndOfDay(getViewCurrentDate().getTime()));

		setViewPeriodFormat("MMMM dd, yyyy");
		dateFormat = new SimpleDateFormat("EEEE", getLocale());
		eventsModel = new WrappingEventsModel(getPeriodStartDate(), getPeriodEndDate(),
								new EventsModel(getPeriodStartDate(), getPeriodEndDate()));
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupCommonComponents()
	 */
	public void setupCommonComponents() {
		dayHeadingLabel = new Label(DAY_HEADING_LABEL_MARKUP_ID, dateFormat.format(getViewCurrentDate().getTime()));
		addOrReplace(dayHeadingLabel);
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupAccessibleComponents()
	 */
	public void setupAccessibleComponents() {

		//Add link to add an event
		addEventLink = new Link(ADD_EVENT_LINK_MARKUP_ID) {
			private static final long serialVersionUID = 1L;

			/**
			 * Change the content to the AddEditEventPanel
			 * @see wicket.markup.html.link.Link#onClick()
			 */
			@Override
			public void onClick() {
				// generate AddEventAction
				DayViewPanel.this.onAction(new AddEventAction(getViewCurrentDate()));
			}
		};
		add(addEventLink);

		dayEventsListView = new EventsListView(EVENT_ITEM_MARKUP_ID, eventsModel, getViewCurrentDate()) {
			private static final long serialVersionUID = 1L;

			@Override
			public void onAction(IAction action) {
				// Check if the action is in the ACTIONS array for this panel
				if (Arrays.asList(DayViewPanel.PANELACTIONS).contains(action.getClass())) {
					// Handle panel actions here
				} else {
					// Pass the action to the parent component
					DayViewPanel.this.onAction(action);
				}
			}
		};
		dayEventsListView.showLongEventDescription(true);
		addOrReplace(dayEventsListView);
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupNonAccessibleComponents()
	 */
	public void setupNonAccessibleComponents() {
		// NOTHING TO DO
	}

	/**
	 * Handles actions generated by this panel.
	 * @param action The action to handle
	 */
	public abstract void onAction(IAction action);
}
